/**
 * 
 */
package com.zufangbao.earth.web.controller.assets;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.InfoMessage;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.zufangbao.sun.entity.company.Company;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.entity.factoring.ArDepositAmtInputType;
import com.zufangbao.sun.entity.factoring.CostDeductType;
import com.zufangbao.sun.entity.factoring.DepositDeductType;
import com.zufangbao.sun.entity.factoring.FactoringContract;
import com.zufangbao.sun.entity.factoring.IntAccureType;
import com.zufangbao.sun.service.AppService;
import com.zufangbao.sun.service.CompanyService;
import com.zufangbao.sun.service.FactoringContractService;

/**
 * @author wk
 *
 */
@Controller
@RequestMapping("/factoring-contracts")
@MenuSetting("menu-data")
public class FactoringContractController {

	@Autowired
	private FactoringContractService factoringContractService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private AppService appService;

	@RequestMapping("")
	@MenuSetting("submenu-assets-factoring-contract")
	public ModelAndView list(Page page) {

		ModelAndView modelAndView = new ModelAndView(
				"factoring-contract/factoring-list");

		List<FactoringContract> factoringContractList = factoringContractService
				.list(FactoringContract.class, page);
		modelAndView.addObject("factoringContractList", factoringContractList);

		return modelAndView;
	}

	@RequestMapping("create")
	@MenuSetting("submenu-assets-factoring-contract")
	public ModelAndView create() {

		ModelAndView modelAndView = new ModelAndView(
				"factoring-contract/factoring-create");

		List<Company> companyList = companyService.loadAll(Company.class);
		List<App> appList = appService.loadAll(App.class);
		modelAndView.addObject("companyList", companyList);
		modelAndView.addObject("appList", appList);

		modelAndView.addObject("IntAccureType", IntAccureType.values());
		modelAndView.addObject("DepositDeductType", DepositDeductType.values());
		modelAndView.addObject("ArDepositAmtInputType",
				ArDepositAmtInputType.values());

		return modelAndView;
	}

	/**
	 * Create factoring-contract
	 * 
	 * @param factoringContract
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	@InfoMessage("info.factoringContract.create.success")
	public String onCreate(@RequestParam("lenderName") Long companyId,
			@RequestParam("borrowerName") Long appId,
			@RequestParam("contractNo") String contractNo,
			@RequestParam("advaPct") String advaPct,
			@RequestParam("settlePct") String settlePct,
			@RequestParam("intRate") String intRate,
			@RequestParam("intAccuretype") int intAccuretype,
			@RequestParam("depositDeductType") int depositDeductType,
			@RequestParam("advaMatuterm") Integer advaMatuterm,
			@RequestParam("arDepositAmtInputType") int arDepositAmtInputType) {

		Company company = companyService.load(Company.class, companyId);
		App app = appService.load(App.class, appId);

		FactoringContract factoringContract = createFactoringContract(company,
				app, contractNo, advaPct, settlePct, intRate, intAccuretype,
				depositDeductType, advaMatuterm, arDepositAmtInputType);

		factoringContractService.create(factoringContract);

		return "redirect:/factoring-contracts";
	}

	public FactoringContract createFactoringContract(Company company, App app,
			String contractNo, String advaPct, String settlePct,
			String intRate, int intAccuretype, int depositDeductType,
			Integer advaMatuterm, int arDepositAmtInputType) {
		FactoringContract factoringContract = create_input_factoring_contract(
        company, app, contractNo, advaPct, settlePct, intRate, intAccuretype,
        depositDeductType, advaMatuterm, arDepositAmtInputType);
		setDefaultValue(factoringContract);
		return factoringContract;
	}

  private FactoringContract create_input_factoring_contract(Company company,
      App app, String contractNo, String advaPct, String settlePct,
      String intRate, int intAccuretype, int depositDeductType,
      Integer advaMatuterm, int arDepositAmtInputType) {
    FactoringContract factoringContract = new FactoringContract();

		factoringContract.setCompany(company);
    factoringContract.setApp(app);
    //factoringContract.setLenderName(company.getName());
    factoringContract.setLenderName(company.getFullName());
    factoringContract.setBorrowerName(app.getName());
    factoringContract.setContractNo(contractNo);
    factoringContract.setAdvaPct(new BigDecimal(advaPct));
    factoringContract.setSettlePct(new BigDecimal(settlePct));
    factoringContract.setIntRate(new BigDecimal(intRate));
    factoringContract.setIntAccuretype(IntAccureType
    		.fromValue(intAccuretype));
    factoringContract.setDepositDeductType(DepositDeductType
    		.fromValue(depositDeductType));
    factoringContract.setAdvaMatuterm(advaMatuterm);
    factoringContract.setArDepositAmtInputType(ArDepositAmtInputType
    		.fromValue(arDepositAmtInputType));
    return factoringContract;
  }

  private void setDefaultValue(FactoringContract factoringContract) {
    factoringContract.setOtsdAmt(new BigDecimal("0"));
		factoringContract.setOtsdAmtAval(new BigDecimal("0"));
		factoringContract.setAdvaStartDate(new Date());
		factoringContract.setArAmt(new BigDecimal("0"));
		factoringContract.setArLdgamt(new BigDecimal("0"));
		factoringContract.setArCostRate(new BigDecimal("0"));
		factoringContract.setArDepositAmt(new BigDecimal("0"));
		factoringContract.setArDepositAmtAval(new BigDecimal("0"));
		factoringContract.setCostDeductType(CostDeductType.InterestReceive);
  }

  
}
